/*
 * Copyright (c) 2017-present, CV4J Contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.core.binary;

import com.cv4j.core.datamodel.ByteProcessor;

import java.util.Arrays;

/**
 * Skeleton
 *
 * @since 2021-07-22
 */
public class Skeleton {
    private boolean dt(byte[] input, byte[] output, int[] distmap, int level, ByteProcessor binary) {
        int width = binary.getWidth();
        int height = binary.getHeight();
        boolean stop = true;
        int p1 = 0;
        int p2 = 0;
        int p3 = 0;
        int p4 = 0;
        int p5 = 0;
        int p6 = 0;
        int p7 = 0;
        int p8 = 0;
        int p9 = 0;
        int offset = 0;
        for (int row = 1; row < height - 1; row++) {
            offset = row * width;
            for (int col = 1; col < width - 1; col++) {
                p1 = input[offset - width + col - 1] & 0xff;
                p2 = input[offset - width + col] & 0xff;
                p3 = input[offset - width + col + 1] & 0xff;
                p4 = input[offset + col - 1] & 0xff;
                p5 = input[offset + col] & 0xff;
                p6 = input[offset + col - 1] & 0xff;
                p7 = input[offset + width + col - 1] & 0xff;
                p8 = input[offset + width + col] & 0xff;
                p9 = input[offset + width + col + 1] & 0xff;
                int sum = p1 + p2 + p3 + p4 + p6 + p7 + p8 + p9;
                int total = 255 * 8;
                if (p5 == 255 && sum != total) {
                    output[offset + col] = (byte) 0;
                    distmap[offset + col] = distmap[offset + col] + level;
                    stop = false;
                }
            }
        }
        return stop;
    }

}
